package thingapi

import (
	//"fmt"
	"iot-base/common/dbconn"
	"iot-base/common/eventtype"
	"iot-base/common/extendobj"
	"iot-base/common/notify"
	"iot-base/common/params"
	"iot-base/common/webapi"

	"time"

	"github.com/gin-gonic/gin"
)

func initAlarm(api *gin.RouterGroup) {
	extendobj.InitAPI(api)
	alarm := api.Group("alarm")
	{
		alarm.GET("/active/", func(c *gin.Context) {
			tid := c.Query("tid")
			var alarms []eventtype.AlarmState
			var err error
			if tid == "" {
				err = dbconn.DBConnection.Where("is_alarm=? and is_hide = false", true).Find(&alarms).Error
			} else {
				err = dbconn.DBConnection.Where("is_alarm=? and tid=? and is_hide = false", true, tid).Error
			}
			if err == nil {
				//var result = make([]*event.Alarm, len(alarms), len(alarms))
				var result = "["
				for i, alarm := range alarms {
					if i == 0 {
						result += string(alarm.AlarmObj)
					} else {
						result += ("," + string(alarm.AlarmObj))
					}

				}
				result += "]"
				webapi.SendText(result, c)
			} else {
				webapi.SendError(err.Error(), webapi.DBError, c)
			}

		})
		alarm.GET("/history/", func(c *gin.Context) {
			tid := c.Query("tid")
			pid := c.Query("pid")
			subid := c.Query("sudid")
			index := c.Query("index")
			beginTime := c.Query("begin")
			endTime := c.Query("end")
			var qend time.Time
			var qbegin time.Time
			if endTime == "" {
				qend = time.Now()
			}
			if beginTime == "" {
				qbegin = qend.Add(-7 * time.Hour * 24)
			}
			condition := ""

			if pid != "" {
				if condition != "" {
					condition = condition + " and pid='" + pid + "'"
				} else {
					condition = "pid='" + pid + "'"
				}
			}
			if tid != "" {
				if condition != "" {
					condition = condition + " and tid='" + tid + "'"
				} else {
					condition = "tid='" + tid + "'"
				}
			}
			if subid != "" {
				if condition != "" {
					condition = condition + " and subid='" + subid + "'"
				} else {
					condition = "subid='" + subid + "'"
				}
			}
			if index != "" {
				if condition != "" {
					condition = condition + " and index=" + index
				} else {
					condition = "index=" + index
				}
			}
			if condition == "" {
				condition = "alarm_time <= ? and alarm_time >= ?"
			} else {
				condition = condition + " and alarm_time <= ? and alarm_time >= ?"
			}

			var alarms []eventtype.AlarmHistroy
			err := webapi.Paginate(c)().Where(condition, qend, qbegin).Find(&alarms).Error
			if err == nil {
				webapi.SendResult(alarms, nil, c)
			} else {
				webapi.SendError(err.Error(), webapi.DBError, c)
			}

		})
	}

}

func initAlarmListenner() {
	notify.SubAlarm(func(palarm *eventtype.Alarm) {
		//fmt.Println("get alarm******************", palarm)
		if !palarm.IsHide {
			server.BroadcastToRoom("", "alarmRoom", "alarm", palarm)
		}

	})
	notify.SubMsg(false, "alarmChanged", nil, func(t string, body interface{}) {
		//fmt.Println("alarm changed -------------------------!!!!!!!!!!!", body)
		server.BroadcastToRoom("", "alarmRoom", "alarmRefresh", nil)
	})
	notify.SubDataReady(func(pValue *params.ValueInRedis) {

		if pValue.IsChanged {
			//fmt.Println("event for int/enum/bool changed-----------------")
			server.BroadcastToRoom("", "eventRoom", "dataChanged", pValue)
		}

	})

}
